//package cn.base.web.gateway;
//
//import cn.base.web.auth.token.AuthenTokenManager;
//import cn.base.web.authc.AccessControl;
//import cn.base.web.authc.DefaultAuthenticator;
//import cn.rengy.auth.Authentication;
//import cn.rengy.auth.AuthenToken;
//import cn.rengy.auth.UserService;
//import cn.rengy.auth.entity.principal.Identity;
//import cn.rengy.auth.exception.AuthException;
//import cn.rengy.auth.pathtree.PathTree;
//import cn.rengy.auth.pathtree.UriTree;
//import cn.rengy.lang.ResultEntity;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.HttpStatus;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//
//import java.util.List;
//
//@Component
//@Slf4j
//public class GatewayAccessControl implements AccessControl {
//
//    @Autowired
//    private AuthenTokenManager authenTokenManager;
//
//    @Autowired
//    private DefaultAuthenticator defaultAuthenticator;
//    @Autowired
//    private UserService userService;
//    @Override
//    public ResultEntity<HttpStatus, Authentication> isAccessAllowed(Object request, Object response) {
//
//        //log.debug("exchange.getRequest().getPath().value()={}",path.value());
//        //log.debug("exchange.getRequest().getURI().getPath()={}",uri.getPath());
//        ServerWebExchange exchange=(ServerWebExchange)request;
//        String path=exchange.getRequest().getURI().getPath();
//        String method=exchange.getRequest().getMethodValue();
//        if(method.equals("OPTIONS")){
//            return ResultEntity.ok(HttpStatus.OK,null);
//        }
//        //log.debug("exchange.getRequest().getMethodValue()={}",method);
//
//        //long startTime = System.nanoTime();
//        //boolean isAllow=false;//是否允许访问
//
//        ResultEntity<PathTree.MatchResult, List<String>> match= UriTree.matchUri(path,method);
//        PathTree.MatchResult matchResult=  match.getCode();
//        //int responseStatus= HttpStatus.UNAUTHORIZED.value();//401
//        //String msg="fail";
//        if(log.isDebugEnabled()){
//            String matchName=matchResult.name();
//            if (matchResult == PathTree.MatchResult.author) {
//                matchName=matchName.concat(match.getData().toString());
//            }
//            log.debug("{}==={}==={}",path,method,matchName);
//        }
//        Identity identity=null;
//        //未找到或匿名访问
//        if(matchResult== PathTree.MatchResult.notFound||matchResult== PathTree.MatchResult.anon){
//            //resultEntity.setOk();
//            return ResultEntity.ok(HttpStatus.OK,null);
//        }else{
//            //method不支持
//            if(matchResult == PathTree.MatchResult.methodNotSupported){
//                //msg="methodNotSupported";
//                return ResultEntity.fail(HttpStatus.METHOD_NOT_ALLOWED,"methodNotSupported");
//                //responseStatus=HttpStatus.METHOD_NOT_ALLOWED.value();
//            }else {//需要登录
//                Authentication authentication=null;
//                try {
//                    AuthenToken authenticationToken=authenTokenManager.getAuthenticationToken(exchange.getRequest());
//                    authentication=defaultAuthenticator.authenticate(authenticationToken);
//
//                }catch(AuthException e) {
//                    //responseStatus = HttpStatus.UNAUTHORIZED.value();//未认证
//                    return ResultEntity.fail(HttpStatus.UNAUTHORIZED,e.getMessage());
//                    //log.info("登录失败:{}",e.getMessage());
//
//                }
//                identity=(Identity)authentication.getPrincipal();
//                Long userId=identity.getUserId();
//                if (matchResult == PathTree.MatchResult.author) {
//                    List<String> pathRoles=match.getData();
//                    if(!userService.hasRoles(userId,pathRoles)){
//                        return ResultEntity.fail(HttpStatus.FORBIDDEN,"没有权限执行此操作");
//                    }
//                }
//                return ResultEntity.ok(HttpStatus.OK,authentication);
//            }
//        }
//    }
//}
